package com.lu.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @program lu-boot-saas
 * @description: mybatis-plus配置
 * @author: zhanglu
 * @create: 2022-09-26 14:25:00
 */
@Configuration
@MapperScan("com.lu.mapper")
public class MybatisPlusConfig {

  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
  }

  @Bean
  public CustomizedSqlInjector customizedSqlInjector() {
    return new CustomizedSqlInjector();
  }

  class CustomizedSqlInjector extends DefaultSqlInjector {

    /**
     * 如果只需增加方法，保留mybatis plus自带方法， 可以先获取super.getMethodList()，再添加add
     */
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
      List<AbstractMethod> methodList = super.getMethodList(mapperClass);
      methodList.add(new MyBatisPlusBatchInsertMethod());
      methodList.add(new MyBatisPlusBatchUpdateMethod());
      return methodList;
    }
  }

}

